
package com.hlkj.pay.app.adminuser.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.LocalContext;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.adminuser.IAdminTeamsAppService;
import com.hlkj.pay.common.AdminUserResultCode;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.dto.LocalAdminUserRequest;
import com.hlkj.pay.enums.AdminUserEnum;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.model.admin.AdminTeamsDO;
import com.hlkj.pay.infrastructure.model.admin.AdminUserDO;
import com.hlkj.pay.service.admin.IAdminTeamsService;
import com.hlkj.pay.service.admin.IAdminUserService;
import com.hlkj.pay.service.common.IBusinessLogService;
import com.hlkj.pay.vo.admin.req.teams.AdminTeamsReq;
import com.hlkj.pay.vo.admin.req.teams.TeamsUserReq;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/11/05 14:57
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdminTeamsAppServiceImpl implements IAdminTeamsAppService {

    private final IAdminTeamsService adminTeamsService;

    private final IAdminUserService adminUserService;
    private final IBusinessLogService businessLogService;
    @Override
    public CommonResult<String> addTeams(AdminTeamsReq adminTeamsReq) {
        log.info("addTeams adminTeamsReq:{}", JsonUtils.toJsonString(adminTeamsReq));
        try {
            AdminTeamsDO adminTeamsDO = adminTeamsService.queryByName(adminTeamsReq.getName());
            if (adminTeamsDO != null) {
                return CommonResult.error(AdminUserResultCode.TEAMS_DOES_EXIST);
            }
            AdminUserDO adminUserDO = adminUserService.queryUser(adminTeamsReq.getOwnId());
            if(adminUserDO==null){
                return CommonResult.error(AdminUserResultCode.USER_DOES_NOT_EXIST);
            }
            adminTeamsDO = new AdminTeamsDO();
            adminTeamsDO.setName(adminTeamsReq.getName());
            adminTeamsDO.setOwnId(adminTeamsReq.getOwnId());
            LocalAdminUserRequest localAdminUserRequest = (LocalAdminUserRequest) LocalContext.get();
            adminTeamsDO.setSn(localAdminUserRequest.getSn());
            adminTeamsDO.setSysType(localAdminUserRequest.getSysType());
            
            adminTeamsService.addTeams(adminTeamsDO);
            Long id = adminTeamsDO.getId();
            String code = String.format("%04d", id);
            adminTeamsDO.setCode(code);
            adminTeamsService.updateTeams(adminTeamsDO);
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("addTeams error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> updateTeams(AdminTeamsReq adminTeamsReq) {
        log.info("updateTeams adminTeamsReq:{}", JsonUtils.toJsonString(adminTeamsReq));
        try {
            AdminTeamsDO adminTeamsDb = adminTeamsService.queryByCode(adminTeamsReq.getCode());
            if (adminTeamsDb == null) {
                return CommonResult.error(AdminUserResultCode.TEAMS_DOES_NOT_EXIST);
            }
            AdminTeamsDO adminTeamsDO = adminTeamsService.queryByName(adminTeamsReq.getName());
            if (adminTeamsDO != null && !adminTeamsDO.getCode().equals(adminTeamsReq.getCode())) {
                return CommonResult.error(AdminUserResultCode.TEAMS_DOES_EXIST);
            }
            AdminUserDO adminUserDO = adminUserService.queryUser(adminTeamsReq.getOwnId());
            if(adminUserDO==null){
                return CommonResult.error(AdminUserResultCode.USER_DOES_NOT_EXIST);
            }
            adminTeamsDO = new AdminTeamsDO();
            adminTeamsDO.setName(adminTeamsReq.getName());
            adminTeamsDO.setOwnId(adminTeamsReq.getOwnId());
            adminTeamsDO.setId(adminTeamsDb.getId());
            adminTeamsService.updateTeams(adminTeamsDO);
            
            businessChangeLog(adminTeamsDb,adminTeamsDO);
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("updateTeams error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> deleteTeams(String code) {
        log.info("deleteTeams code:{}", code);
        try {
            AdminTeamsDO adminTeamsDb = adminTeamsService.queryByCode(code);
            if (adminTeamsDb == null) {
                return CommonResult.error(AdminUserResultCode.TEAMS_DOES_NOT_EXIST);
            }
            adminTeamsService.deleteTeams(adminTeamsDb.getId());
            AdminTeamsDO adminTeamsDO= BeanUtil.copyProperties(adminTeamsDb, AdminTeamsDO.class);
            adminTeamsDO.setDeleteFlag(true);
            businessChangeLog(adminTeamsDb,adminTeamsDO);
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("deleteTeams error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> assignTeamsUser(TeamsUserReq teamsUserReq) {
        log.info("assignTeamsUser teamsUserReq:{}", JsonUtils.toJsonString(teamsUserReq));
        try {
            AdminTeamsDO adminTeamsDb = adminTeamsService.queryByCode(teamsUserReq.getCode());
            if (adminTeamsDb == null) {
                return CommonResult.error(AdminUserResultCode.TEAMS_DOES_NOT_EXIST);
            }
            adminUserService.updateUserTeams(teamsUserReq.getUserIds(),teamsUserReq.getCode());
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("assignTeamsUser error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> removeTeamsUser(TeamsUserReq teamsUserReq) {
        log.info("removeTeamsUser teamsUserReq:{}", JsonUtils.toJsonString(teamsUserReq));
        try {
            AdminTeamsDO adminTeamsDb = adminTeamsService.queryByCode(teamsUserReq.getCode());
            if (adminTeamsDb == null) {
                return CommonResult.error(AdminUserResultCode.TEAMS_DOES_NOT_EXIST);
            }
            adminUserService.updateUserTeams(teamsUserReq.getUserIds(),"");
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("removeTeamsUser error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }
    /**
     * log object
     *
     * @param beforeTeams
     * @param afterTeams
     * @return
     */
    void businessChangeLog(AdminTeamsDO beforeTeams, AdminTeamsDO afterTeams) {
        businessLogService.addBusinessChangeLog(beforeTeams.getCode(), AdminUserEnum.CHANGE_LOG_TYPE.TEAMS.getCode(), CommonEnum.CHANGE_LOG_MODEL_TYPE.COMMON, beforeTeams,
                afterTeams);
    }
}
